package com.example.tests;


import java.util.Random;

import org.testng.annotations.Test;

import com.example.framework.Folders;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.testng.AssertJUnit.*;

public class TestFolderCreationAndRemoval extends TestBase {
	
	@Test
	public void testFolderCreation() {
		String folder = "newFolder1";
		Folders oldFolders = app.getFolderHelper().getFolders();
		app.getFolderHelper().createFolder(folder);
		Folders newFolders = app.getFolderHelper().getFolders();
		assertThat(newFolders, equalTo(oldFolders.withAdded(folder)));
	}
	
	@Test
	public void testVariousFoldersCreation() {
		String folder1 = "newFolder2";
		String folder2 = "newFolder3";
		Folders oldFolders = app.getFolderHelper().getFolders();
		assertNull(app.getFolderHelper().createFolder(folder1));
		Folders newFolders = app.getFolderHelper().getFolders();
		assertThat(newFolders, equalTo(oldFolders.withAdded(folder1)));
		assertNull(app.getFolderHelper().createFolder(folder2));
		Folders newFolders2 = app.getFolderHelper().getFolders();
		assertThat(newFolders2, equalTo(newFolders.withAdded(folder2)));
	}

	@Test
	public void testFoldersWithSameNameCreation() {
		String folder = "newFolder4";
		Folders oldFolders = app.getFolderHelper().getFolders();
		assertNull(app.getFolderHelper().createFolder(folder));
		Folders newFolders = app.getFolderHelper().getFolders();
		assertThat(newFolders, equalTo(oldFolders.withAdded(folder)));
		assertThat(app.getFolderHelper().createFolder(folder), containsString("Duplicated folder name"));
		Folders newFolders2 = app.getFolderHelper().getFolders();
		assertThat(newFolders2, equalTo(newFolders));
	}
	
	@Test
	public void testRemoveRandomFolder() throws InterruptedException {
		Folders oldFolders = app.getFolderHelper().getFolders();
		int index = new Random().nextInt(oldFolders.size());
		app.getFolderHelper().removeFolder(index);
		Folders newFolders = app.getFolderHelper().getFolders();
		assertThat(newFolders, equalTo(oldFolders.without(index)));
	}
	

}
